package com.wifi;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;

import com.wifi.eshipewifi.SocThread;

/**
 * Created by Administrator on 2016/8/11.
 */
public class BaseApplicationV3 extends Application {

    public String ip=null;
    public String ssid;
    public String password;
    SharedPreferences sp;
    @Override
    public void onCreate() {
        super.onCreate();

        sp= getSharedPreferences("wifiSetting", Activity.MODE_PRIVATE);
        ssid = sp.getString("ssid", "sn1111111");//todo 以后要改,默认账号 sn1111111要改
        password = sp.getString("password", "czd5d323");
    }

    public String getSsid(){
        ssid = sp.getString("ssid", "sn1111111");//todo 以后要改,默认账号 sn1111111要改
        return ssid;
    }



    public void setSsid(String ssid){
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("ssid",ssid);
        edit.commit();
    }

    public void setPassword(String password){
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("password",password);
        edit.commit();
    }

    public String getPassword(){
        if(password==null)
            password = sp.getString("password", "czd5d323");
        return password;
    }

    public String getIp(){
//        WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
//        DhcpInfo di = wm.getDhcpInfo();
//        long getewayIpL = di.gateway;
//        String getwayIpS = long2ip(getewayIpL);//网关地址
        if(ip==null)
            ip = sp.getString("ip", "192.168.8.1");
        return ip;
    }

    public void setIp(String ip){
        this.ip=ip;
    }

    private SocThread thread=null;
    public void setThread(SocThread thread){
        this.thread=thread;
    }
    public SocThread getThread(Handler handlerin, Handler handlerout, Context context){
        if(thread==null){
            thread=new SocThread(handlerin,handlerout,context);
        }
        thread.setHandler(handlerin,handlerout);//重置handler
        return thread;
    }


    String long2ip(long ip) {
        StringBuffer sb = new StringBuffer();
        sb.append(String.valueOf((int) (ip & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 8) & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 16) & 0xff)));
        sb.append('.');
        sb.append(String.valueOf((int) ((ip >> 24) & 0xff)));
        return sb.toString();
    }



}
